package com.zy.is.common.api;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 接口统一返回对象
 *
 * @author Yanzy
 * @date 2021-04-03 17:30
 */
@Setter
@Getter
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -3135108159811954608L;
    /**
     * 返回值
     */
    private long code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 返回对象
     */
    private T data;

    /**
     * 失败响应
     *
     * @param message 消息
     * @return r
     */
    public static <T> Result<T> error(String message) {
        return result(ApiConstants.ERR_CODE, message, null);
    }

    public static <T> Result<T> error(String message, T data) {
        return result(ApiConstants.ERR_CODE, message, data);
    }

    /**
     * 成功响应
     *
     * @param message 消息
     * @return r
     */
    public static <T> Result<T> success(String message) {
        return result(ApiConstants.OK_CODE, message, null);
    }

    public static <T> Result<T> success(String message, T data) {
        return result(ApiConstants.OK_CODE, message, data);
    }


    private static <T> Result<T> result(long code, String message, T data) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);
        return r;
    }
}
